package com.ymatou.query.seller.trading.api.testcase;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.query.seller.trading.api.parameter.GetSellerOrderStatusCountBean;
import com.ymatou.query.seller.trading.api.parameter.TimeRange;
import com.ymatou.query.seller.trading.api.service.GetSellerOrderStatusCountCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.common.enums.OrderStatusEnum;
import com.ymttest.database.sqlwapper.YmtOrdersWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.YMTDateUtil;
import com.ymttest.utils.constant.ECode;

/**
 * APP卖家获取订单状态统计数据
 */
@TestSuite
public class Ts_GetSellerOrderStatusCount {

	private static GetSellerOrderStatusCountBean getSellerOrderStatusCountBean;
	private static GetSellerOrderStatusCountCall getSellerOrderStatusCountCall;
	private static YmtOrdersWapper ymtOrdersWapper;


	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("APP卖家获取订单状态统计数据");
	}

	@Before
	public void caseUp() {
		getSellerOrderStatusCountBean = new GetSellerOrderStatusCountBean();
		getSellerOrderStatusCountCall = new GetSellerOrderStatusCountCall();


		ymtOrdersWapper = new YmtOrdersWapper();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getSellerOrderStatusCountCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	public void verify() throws JSONException {
		int SellerId = getSellerOrderStatusCountBean.getSellerId();
		TimeRange AddTime = getSellerOrderStatusCountBean.getAddTime();
		List<Integer> OrderStatusList = getSellerOrderStatusCountBean
				.getOrderStatusList();
		StringBuffer sql = new StringBuffer();
		sql.append(" o.iBuyerId = " + SellerId);
		JSONObject Result = getSellerOrderStatusCountCall.getData()
				.getJSONObject("Result");
		if (null != AddTime) {
			String Begin = AddTime.getBegin();
			String End = AddTime.getEnd();
			sql.append(" and o.dAddTime >= '" + Begin + "' and o.dAddTime < '"
					+ End + "'");
		}
		if (null != OrderStatusList) {
			verifyOrderCountGroupByStatus(OrderStatusList, sql, Result);
		} else {
			OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(1);
			OrderStatusList.add(2);
			OrderStatusList.add(3);
			OrderStatusList.add(4);
			OrderStatusList.add(12);
			OrderStatusList.add(13);
			OrderStatusList.add(16);
			OrderStatusList.add(17);
			OrderStatusList.add(18);
			OrderStatusList.add(101);
			verifyOrderCountGroupByStatus(OrderStatusList, sql, Result);
		}
		int expectCount = ymtOrdersWapper.selectAppSellerOrdersTestData(
				Integer.MAX_VALUE, sql.toString()).size();
		Logger.verifyEquals(expectCount, Result.get(String.valueOf(0)),
				"验证订单总数");
	}

	private void verifyOrderCountGroupByStatus(List<Integer> OrderStatusList,
			StringBuffer sql, JSONObject Result) throws JSONException {
		String statusCondition = "";
		for (Integer status : OrderStatusList) {
			if (status == OrderStatusEnum.OrderPostPay.getValue()) {
				statusCondition = " and o.[iTradingStatus] = " + 17
						+ " and o.bPaidInFull = 0";
			} else if (status == OrderStatusEnum.SellerAccept.getValue()) {
				statusCondition = " and o.[iTradingStatus] = " + 17
						+ " and o.bPaidInFull = 1";
			} else {
				statusCondition = " and o.[iTradingStatus] = " + status;
			}
			verifyCount(sql, status, Result, statusCondition);
		}
	}

	private void verifyCount(StringBuffer sql, Integer status,
			JSONObject Result, String statusCondition) throws JSONException {
		int expectCount = ymtOrdersWapper.selectAppSellerOrdersTestData(
				Integer.MAX_VALUE, sql.toString() + statusCondition).size();
		if (0 == expectCount) {
			Logger.verifyEquals(true, Result.isNull(String.valueOf(status)),
					"验证订单状态为" + status + "的总数不存在");
		} else {
			Logger.verifyEquals(expectCount,
					Result.get(String.valueOf(status)), "验证订单状态为" + status
							+ "的总数");
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderStatusCount_001() {
		Logger.start(true, "APP卖家获取订单状态统计数据成功,只传SellerId");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 卖家ID
			getSellerOrderStatusCountBean.setSellerId(SellerId);

			// GetSellerOrderStatusCount 后台发送请求
			getSellerOrderStatusCountCall
					.setData(getSellerOrderStatusCountBean);
			getSellerOrderStatusCountCall.callService();
			Logger.comment("getSellerOrderStatusCountCall send:"
					+ getSellerOrderStatusCountCall.getEntityString());
			Logger.comment("getSellerOrderStatusCountCall response:"
					+ getSellerOrderStatusCountCall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getSellerOrderStatusCountCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderStatusCount_002() {
		Logger.start(true,
				"APP卖家获取订单状态统计数据成功，传SellerId, AddTime, OrderStatusList为部分订单状态");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 参数
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-60);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1);
			TimeRange addTime = new TimeRange();
			addTime.setBegin(BeginTime);
			addTime.setEnd(EndTime);
			getSellerOrderStatusCountBean.setAddTime(addTime);
			// 订单状态
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(1);
			OrderStatusList.add(2);
			OrderStatusList.add(3);
			OrderStatusList.add(4);
			getSellerOrderStatusCountBean.setOrderStatusList(OrderStatusList);
			// 卖家ID
			getSellerOrderStatusCountBean.setSellerId(SellerId);

			// GetSellerOrderStatusCount 后台发送请求
			getSellerOrderStatusCountCall
					.setData(getSellerOrderStatusCountBean);
			getSellerOrderStatusCountCall.callService();
			Logger.comment("getSellerOrderStatusCountCall send:"
					+ getSellerOrderStatusCountCall.getEntityString());
			Logger.comment("getSellerOrderStatusCountCall response:"
					+ getSellerOrderStatusCountCall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getSellerOrderStatusCountCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderStatusCount_003() {
		Logger.start(true,
				"APP卖家获取订单状态统计数据成功，传SellerId, OrderStatusList为全部订单状态");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 订单状态
			ArrayList<Integer> OrderStatusList = new ArrayList<Integer>();
			OrderStatusList.add(1);
			OrderStatusList.add(2);
			OrderStatusList.add(3);
			OrderStatusList.add(4);
			OrderStatusList.add(12);
			OrderStatusList.add(13);
			OrderStatusList.add(16);
			OrderStatusList.add(17);
			OrderStatusList.add(18);
			OrderStatusList.add(101);
			getSellerOrderStatusCountBean.setOrderStatusList(OrderStatusList);
			// 卖家ID
			getSellerOrderStatusCountBean.setSellerId(SellerId);

			// GetSellerOrderStatusCount 后台发送请求
			getSellerOrderStatusCountCall
					.setData(getSellerOrderStatusCountBean);
			getSellerOrderStatusCountCall.callService();
			Logger.comment("getSellerOrderStatusCountCall send:"
					+ getSellerOrderStatusCountCall.getEntityString());
			Logger.comment("getSellerOrderStatusCountCall response:"
					+ getSellerOrderStatusCountCall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getSellerOrderStatusCountCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderStatusCount_004() {
		Logger.start(true, "APP卖家获取订单状态统计数据成功，传SellerId, AddTime");
		try {
			int SellerId = Integer.valueOf(EnvSetup.getData("SellerId"));
			// 参数
			String BeginTime = YMTDateUtil.getBeforeOrNextDay(-92);
			String EndTime = YMTDateUtil.getBeforeOrNextDay(1);
			TimeRange addTime = new TimeRange();
			addTime.setBegin(BeginTime);
			addTime.setEnd(EndTime);
			getSellerOrderStatusCountBean.setAddTime(addTime);
			// 卖家ID
			getSellerOrderStatusCountBean.setSellerId(SellerId);

			// GetSellerOrderStatusCount 后台发送请求
			getSellerOrderStatusCountCall
					.setData(getSellerOrderStatusCountBean);
			getSellerOrderStatusCountCall.callService();
			Logger.comment("getSellerOrderStatusCountCall send:"
					+ getSellerOrderStatusCountCall.getEntityString());
			Logger.comment("getSellerOrderStatusCountCall response:"
					+ getSellerOrderStatusCountCall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getSellerOrderStatusCountCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderStatusCount_005() {
		Logger.start(true, "APP卖家获取订单状态统计数据失败，传SellerId=0");
		try {
			int SellerId = 0;
			// 卖家ID
			getSellerOrderStatusCountBean.setSellerId(SellerId);

			// GetSellerOrderStatusCount 后台发送请求
			getSellerOrderStatusCountCall
					.setData(getSellerOrderStatusCountBean);
			getSellerOrderStatusCountCall.callService();
			Logger.comment("getSellerOrderStatusCountCall send:"
					+ getSellerOrderStatusCountCall.getEntityString());
			Logger.comment("getSellerOrderStatusCountCall response:"
					+ getSellerOrderStatusCountCall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getSellerOrderStatusCountCall.getCode(), "验证Code");
			verify();
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetSellerOrderStatusCount_006() {
		Logger.start(true, "APP卖家获取订单状态统计数据失败，传SellerId=null");
		try {
			// 卖家ID
			getSellerOrderStatusCountBean.setSellerId(null);

			// GetSellerOrderStatusCount 后台发送请求
			getSellerOrderStatusCountCall
					.setData(getSellerOrderStatusCountBean);
			getSellerOrderStatusCountCall.callService();
			Logger.comment("getSellerOrderStatusCountCall send:"
					+ getSellerOrderStatusCountCall.getEntityString());
			Logger.comment("getSellerOrderStatusCountCall response:"
					+ getSellerOrderStatusCountCall.getReturnData());
			Logger.verifyEquals(ECode.SUCCESS,
					getSellerOrderStatusCountCall.getCode(), "验证Code");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

}